<template>
	<div>
		<div class="h2">体测项目管理</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="项目名称"><a-input autocomplete="off" class="field-right" placeholder="请输入项目名称搜索" v-decorator="[`key`]" /></a-form-item>
					</a-col>
					<a-col :span="8" style="margin-top:4px;">
						<a-button type="primary" html-type="submit" class="btn1">搜索</a-button>
						<a-button :style="{ marginLeft: '8px' }" @click="handleReset" class="btn2">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div class="option">
			<a-button type="primary" class="btn" @click="addList">新增</a-button>
		</div>
		<div>
			<el-table ref="tableList" :data="data" v-loading="loading" border :max-height="tableHeight" highlight-current-row style="width: 100%;">
				<el-table-column prop="Title" label="项目名称" min-width="120" fixed="left"></el-table-column>
				<el-table-column prop="AddUserName" label="添加人" min-width="100">
					<template slot-scope="scope">
						{{ scope.row.AddUserNo }} / {{ scope.row.AddUserName }}
					</template>
				</el-table-column>
				<el-table-column prop="AddTime" label="添加时间" width="160"></el-table-column>
				<el-table-column label="操作" width="120" fixed="right">
					<template slot-scope="scope">
						<i title="成绩查询" class="iconfont icon-xiangqing" style="color: blue;" @click="seeList(scope.row)"></i>
						<i title="修改" class="iconfont icon-editor1" style="color: #1890FF;" @click="modifyList(scope.row, 'modify')"></i>
						<i title="删除" class="iconfont icon-delete" style="color: red;" @click="deleteList(scope.row, 1)"></i>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pagination.currentPage"
				:page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"
			></el-pagination>
		</div>
		<add :text="text" v-show="addIf" @closeFun="closeFun"></add>
	</div>
</template>

<script>
import add from './add';
import Func from '@/utils/func';
import moment from 'moment';
import Cache from '@/utils/cache';
import { DoGetUserTestPageList, DoDeleteUserTest } from '@/api/follow';
const data = [];
export default {
	components: {
		add
	},
	data() {
		return {
			data,
			form: this.$form.createForm(this, {
				name: 'advanced_search'
			}),
			pagination: {
				currentPage: 1,
				pageSize: this.global.pageSize,
				pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
				showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
			},
			addIf: false,
			text: {},
			visible: false,
			confirmLoading: false,
			loading: false,
			values: {},
			tableHeight: parseFloat(window.innerHeight - 160),
			requestUrl: this.global.requestUrl,
			token: Cache.get('token'),
		};
	},
	computed: {},
	created() {
		this.getList();
	},
	mounted() {},
	methods: {
		moment,
		handleSearch(e) {
			e.preventDefault();
			this.form.validateFields((error, values) => {
				this.pagination.currentPage = 1;
				this.values = values;
				this.getList();
			});
		},
		handleReset() {
			this.form.resetFields();
			this.values = {};
			this.getList();
		},
		handleSizeChange(val) {
			this.pagination.pageSize = val;
			this.getList();
		},
		handleCurrentChange(val) {
			this.pagination.currentPage = val;
			this.getList();
		},
		async getList() {
			this.loading = true;
			let data = {};
			if (this.values) {
				data = this.values;
			}
			data.pageIndex = this.pagination.currentPage;
			data.pageSize = this.pagination.pageSize;
			let res = await DoGetUserTestPageList(data);
			this.loading = false;
			this.data = res.data.data;
			const pagination = { ...this.pagination };
			pagination.total = res.data.pageInfo.TotalItemCount;
			this.pagination = pagination;
		},
		addList() {
			this.text = {};
			this.addIf = !this.addIf;
		},
		async handleOk(e) {
			e.preventDefault();
			this.updateForm.validateFields(async (err, values) => {
				if (!err) {
					this.confirmLoading = true;
					let data = {};
					data = values;
					let res = await CoreUpdateStudent(data);
					if (res.data.code == 1) {
						this.$message.success(res.data.msg);
					} else {
						this.$message.error(res.data.msg);
					}
					this.updateForm.resetFields()
					this.visible = false;
					this.confirmLoading = false;
				}
			});
		},
		handleCancel(e) {
			this.visible = false;
			this.updateForm.resetFields()
		},
		modifyList(text) {
			this.text = {...text,time:new Date()}
			this.addIf = !this.addIf;
		},
		deleteList(text, type) {
			let that = this;
			this.$confirm({
				title: '提示',
				content: '您确定删除该体测项目吗?',
				okText: '确认',
				cancelText: '取消',
				async onOk() {
					let data = {};
					data.id = text.ID;
					let res = await DoDeleteUserTest(data)
					if (res.data.code == 0) {
						that.getList();
						that.$message.success(res.data.msg);
					} else {
						that.$message.error(res.data.msg);
					}
				}
			});
		},
		seeList(text, name) {
			this.$router.push({
				name: 'PhysicalTestList',
				query: {
					ID: text.ID
				}
			});
		},
		exportList() {
			this.form.validateFields( async (error, values) => {
				let that = this
				let data = {};
				data.key = values.key;
				data.classID = values.classID;
				data.departCode = values.departCode;
				data.isAssistant = values.isAssistant;
				data.PageSize = 1
				let res = await GetStudentListManage(data);
				if(res.data.pageInfo.TotalItemCount){
					this.$confirm({
						title: '提示',
						content: '当前要导出的数据大于10000，耗时较久，建议您先进行搜索条件检索，再按需导出！',
						okText: '继续导出',
						cancelText: '按需导出',
						async onOk() {
							let hrefUrl = that.requestUrl + 'Manage/Student/DoExport?userToken=' + encodeURIComponent(that.token);
							let hrefStr = '';
							for (let key in data) {
								if (data[key]) {
									hrefStr += key + '=' + data[key] + '&';
								}
							}
							window.open(hrefUrl + '&' + hrefStr, '_blank');
						}
					});
				}else{
					let hrefUrl = that.requestUrl + 'Manage/Student/DoExport?userToken=' + encodeURIComponent(that.token);
					let hrefStr = '';
					for (let key in data) {
						if (data[key]) {
							hrefStr += key + '=' + data[key] + '&';
						}
					}
					window.open(hrefUrl + '&' + hrefStr, '_blank');
				}
			});
		},
		closeFun(data) {
			this.addIf = false;
			if(data==1){
				this.getList();
			}
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form-item {
	display: flex;
	margin-bottom: 10px !important;

	/deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}
	/deep/.ant-form-item-control-wrapper {
		flex: 1;
		/deep/.ant-calendar-picker {
			width: 100%;
		}
	}
}
/deep/.el-table th.is-leaf {
	background: #fafafa;
}
/deep/.el-table td,
/deep/.el-table th {
	padding: 5px;
}
.option {
	margin-top: 10px;
	margin-bottom: 20px;
	overflow: hidden;
}
.option .btn {
	height: 36px;
	line-height: 36px;
	padding: 0 10px;
	float: left;
	background: #1890ff;
	color: #fff;
	margin-right: 10px;
	border-radius: 3px;
	cursor: pointer;
	border: none;
}
.foot {
	text-align: center;
	margin-top: 100px;
}
.el-pagination {
	margin-top: 10px;
}
/deep/ .ant-form-item-control-wrapper {
	min-width: 200px;
}
</style>
